<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_business_demand_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select color" style="padding: 0 20px;">
        <el-form label-position="right" label-width="auto">
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item :label="i18n('demand.description')">
                <el-input v-model="dataForm.description" :placeholder="$t('common.please_input') + $t('demand.description')"  clearable @change="getDataList(model)"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item :label="i18n('demand.demand_time')">
                <el-radio-group v-model="dataForm.demand_time" @change="getDataList(model)">
                  <el-radio label="">{{ i18n('common.all') }}</el-radio>
                  <el-radio v-for="(v, k) in demandYearList" :key="k" :label="v.value">
                    {{ v.text }}
                    <span class="red">({{ v.total }})</span>
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item :label="i18n('demand.demand_type')">
                <el-select
                  v-model="dataForm.demand_type"
                  :placeholder="i18n('common.please_select') + i18n('demand.demand_type')" clearable @change="getDataList(model)">
                  <el-option v-for="(v, k) in demandTypeList" :label="v.text" :key="k" :value="v.value">
                    <span class="float-left">{{ v.text }}</span>
                    <span class="float-right">{{ v.total }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item :label="i18n('demand.demand_status')">
                <el-radio-group v-model="dataForm.demand_status" @change="getDataList(model)">
                  <el-radio label="">{{ i18n('common.all') }}</el-radio>
                  <el-radio v-for="(v, k) in demandStatusList" :key="k" :label="v.value">
                    {{ v.text }}
                    <span class="red">({{ v.total }})</span>
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item :label="i18n('demand.salesperson')">
                <el-select
                  v-model="dataForm.user_id"
                  :placeholder="i18n('common.please_select') + i18n('demand.salesperson')" clearable @change="getDataList(model)">
                  <el-option v-for="(v, k) in userList" :label="v.nickname" :key="k" :value="v.id">
                    <span class="float-left">{{ v.nickname }}</span>
                    <span class="float-right">{{ v.total }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item :label="i18n('demand.money')">
                <el-radio-group v-model="dataForm.money" @change="getDataList(model)">
                  <el-radio label="">{{ i18n('common.all') }}</el-radio>
                  <el-radio v-for="(v, k) in expectedProfitList" :key="k" :label="v.value">
                    {{ v.text }}
                    <span class="red">({{ v.total }})</span>
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="admin_table_main color">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="i18n('demand.demand_type')" width="100">
            <template v-slot="scope">
              <dl class="table_dl">
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.demand_type.text }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.demand_time }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column prop="money" :label="i18n('demand.money')" width="100">
            <template v-slot="scope">
              <span v-if="scope.row.money">
                {{ scope.row.money }}元
              </span>
              <span v-else>
                未知
              </span>
            </template>
          </el-table-column>

          <el-table-column :label="$t('demand.client_contact')">
            <template v-slot="scope">
              <dl class="table_dl">
                <dd class="table_dl_dd_all_30" v-if="scope.row.client">
                  <el-link @click="router.push({ name: 'module_business_client_list_view', query: { id: scope.row.client.id } })" :underline="false">
                    {{ scope.row.client.name }}
                    <el-icon class="mr5"><ArrowRight /></el-icon>
                  </el-link>
                </dd>
                <dd class="table_dl_dd_all_16_gray" v-if="scope.row.contact">
                  <el-link @click="router.push({ name: 'module_business_contact_list_view', query: { id: scope.row.contact.id } })" :underline="false">
                    {{ scope.row.contact.name }}
                    <el-icon class="mr5"><ArrowRight /></el-icon>
                  </el-link>
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column prop="description" :label="i18n('demand.description')" min-width="200">
            <template v-slot="scope">
              <p class="show-2-line">{{ scope.row.description }}</p>
            </template>
          </el-table-column>

          <el-table-column :label="$t('demand.demand_status')" width="100">
            <template v-slot="scope">
              <span :class="scope.row.demand_status.value == 1 ? 'orange' : (scope.row.demand_status.value == 2 ? 'green1' : (scope.row.demand_status.value == 3 ? 'blue' : 'red'))">
                {{ scope.row.demand_status.text }}
              </span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('demand.salesperson')" width="150">
            <template v-slot="scope">
              <dl class="table_dl" v-if="scope.row.user">
                <dt>
                  <el-avatar :size="45" :src="scope.row.user.avatar" />
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.user.nickname }}
                </dd>
                <dd class="table_dl_dd_all_16_gray" v-if="scope.row.user.detail">
                  {{ scope.row.user.detail.position }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="290">
            <template v-slot="scope">
              <el-button
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_business_demand_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>

              <el-dropdown class="more">
                <el-button icon="More">
                  {{ i18n('common.more') }}
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="router.push({ name: 'module_business_demand_list_view', query: { id: scope.row.id } })">
                      {{ i18n('demand.view') }}
                    </el-dropdown-item>

                    <el-dropdown-item :disabled="(scope.row.demand_status.value == 0 || scope.row.demand_status.value == 1) ? false : true" @click="changeHandle(scope.row.id, 2)">需求成交</el-dropdown-item>
                    <el-dropdown-item :disabled="(scope.row.demand_status.value == 0 || scope.row.demand_status.value == 1) ? false : true" @click="changeHandle(scope.row.id, 3)" divided>需求取消</el-dropdown-item>
                    <el-dropdown-item :disabled="(scope.row.demand_status.value == 2 || scope.row.demand_status.value == 3 || scope.row.demand_status.value == 4) ? true : false" @click="router.push({ name: 'module_business_demand_list_lose', query: { demand_id: scope.row.id } })">
                      需求丢单
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="demand-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/demand')

  const demandTypeList = ref([])
  const demandStatusList = ref([])
  const demandYearList = ref([])
  const expectedProfitList = ref([])
  const userList = ref([])

  const dataForm = reactive([
    'description',
    'demand_type',
    'demand_time',
    'demand_status',
    'demand_month',
    'money',
    'user_id',
    'is_recommend',
    'is_hidden',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadSearchList = () => {
    $http({
      url: $http.adornUrl('/business/demand/search/data'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({data}) => {
      if (data && data.status === 200) {
        demandTypeList.value = data.data.demand_type
        demandYearList.value = data.data.demand_year
        demandStatusList.value = data.data.demand_status
        expectedProfitList.value = data.data.expected_profit
        userList.value = data.data.user
      } else {
        $message.error(data.message)
      }
    })
  }

  // 更改客户所属人
  const changeHandle = (id, status) => {
    $http({
      url: $http.adornUrl(`${model.value}/demand_status`),
      method: 'post',
      data: {
        id: id,
        status: status
      },
    }).then(({ data }) => {
      if (data && data.status === 200) {
        $message({
          message: i18n('common.handle_success'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            getDataList()
          },
        })
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    dataForm.client_id = query.client_id

    getDataList(model)

    loadSearchList()
  })
</script>
